<template>
    <div>
        <div class="login">
            <el-form :model="loginData" :rules="loginRules" label-width="80px" ref="loginForm">
                <el-form-item label="账号" prop="userName">
                    <el-input clearable maxlength="10" placeholder="请输用户名" prefix-icon="el-icon-user" show-word-limit v-model="loginData.userName"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input clearable maxlength="32" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password show-word-limit v-model="loginData.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button :disabled="loginBtnDisable" @click="submitForm()" class="login-btn" ref="loginBtn" type="primary">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import axios from "axios"

    export default {
        name: "Index",
        data() {
            return {
                loginBtnDisable: false,
                loginData: {
                    userName: "aaaaaaaaa",
                    password: "bbbbbbbbbb"
                },
                loginRules: {
                    userName: [
                        // {
                        //     type: "string",
                        //     required: true,
                        //     message: "请输用户名",
                        //     trigger: "blur"
                        // },
                        // {
                        //     type: "string",
                        //     required: true,
                        //     message: "长度在 3 到 10 个字符",
                        //     trigger: "blur",
                        //     min: 3,
                        //     max: 10,
                        // },
                        {
                            type: "string",
                            trigger: "blur",
                            validator: (rule, value, callback) => {
                                // console.log("-" + value + "-");
                                value = value.trim();
                                if (value === "") {
                                    return callback(new Error("请输用户名"));
                                }
                                if (value.length < 3 || value.length > 10) {
                                    this.loginData.userName = value
                                    return callback(new Error("长度在 3 到 10 个字符"));
                                }
                                callback()
                            }
                        }
                    ],
                    password: [
                        {
                            type: "string",
                            trigger: "blur",
                            validator: (rule, value, callback) => {
                                // console.log("-" + value + "-");
                                value = value.trim();
                                if (value === "") {
                                    return callback(new Error("请输入密码"));
                                }
                                if (value.length < 6 || value.length > 32) {
                                    this.loginData.password = value
                                    return callback(new Error("长度在 6 到 32 个字符"));
                                }
                                callback()
                            }
                        }
                    ]

                }
            }
        },
        methods: {
            submitForm() {
                // this.loginBtnDisable = true
                this.$refs["loginForm"].validate((valid) => {
                    if (valid) {
                        // axios.get("http://192.168.0.220:9090/user/login", {params: this.loginData}).then(res => {
                        //     console.log(res);
                        // }).catch(reason => {
                        //     console.log(reason);
                        // })
                        axios({
                            url: "http://192.168.0.220:9090/user/login",
                            method: "post",
                            data: this.loginData,
                            // headers: {
                            //     "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                            //     // "Content-Type": "application/json"
                            // }
                        });
                        // axios({
                        //     url: 'http://192.168.0.220:9090/user/login',
                        //     method: 'post'
                        // });
                    } else {
                        console.log("error submit!!");
                        // this.loginBtnDisable = false
                        return false;
                    }
                })
            },
        }
    }
</script>

<style scoped>
    .login {
        width: 400px;
        height: 200px;
    }

    .login-btn {
        width: 320px;
    }
</style>
